//
//  SelectCityViewController.m
//  beautyAccompany
//
//  Created by xiaocankeji on 15/11/17.
//  Copyright © 2015年 xiaocankeji. All rights reserved.
//

#import "SelectCityViewController.h"
#import "CitySelectTableViewCell.h"

@interface SelectCityViewController ()<UITableViewDataSource,UITableViewDelegate,CitySelectTableViewCellDelegate>
@property (nonatomic, strong)UITableView *tableView;
@property (nonatomic, strong)NSArray *hotArr;
@property (nonatomic, strong)NSArray *cityArr;
@property (nonatomic, strong)NSArray *keyArr;
@property (nonatomic, strong)NSMutableDictionary *cityGroupDic;
@end

@implementation SelectCityViewController

- (NSMutableDictionary *)cityGroupDic
{
    if (_cityGroupDic == nil) {
        _cityGroupDic = [NSMutableDictionary dictionary];
    }
    return _cityGroupDic;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"选择城市";
    self.view.backgroundColor = [UIColor whiteColor];
    
    _hotArr = @[@"广州",@"北京",@"上海",@"天津"];
    _cityArr = @[@"广州",@"北京",@"上海",@"天津"];
    for (NSString *city in _cityArr) {
        if(city.length){
            //汉字转拼音
            NSString *spellString = [self transformToSpellFormChinesName:city];
            //获取首字母
            NSString *firstCharac = [spellString substringToIndex:1];
            //转换为大写
            NSString *upperString = [firstCharac uppercaseString];
            NSMutableArray *groupArr = [NSMutableArray arrayWithArray:[self.cityGroupDic objectForKey:upperString]];
            [groupArr addObject:city];
            [self.cityGroupDic setObject:groupArr forKey:upperString];
            
        }
    }
    //对key进行排序
    NSArray *allkeys = [[self.cityGroupDic allKeys] sortedArrayUsingSelector:@selector(compare:)];
    NSMutableArray *keys = [NSMutableArray arrayWithArray:allkeys];
    [keys insertObject:@"$" atIndex:0];
    [keys insertObject:@"#" atIndex:0];
    self.keyArr = [NSArray arrayWithArray:keys];
    
    

    
//    NSString *path= [[NSBundle mainBundle] pathForResource:plistName ofType:@"plist"];
//    NSArray * array=[[NSArray alloc] initWithContentsOfFile:path];
    // Do any additional setup after loading the view.
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, DeviceWidth, DeviceHeight) style:UITableViewStylePlain];
    _tableView.backgroundColor = [UIColor clearColor];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.tableFooterView = [[UIView alloc]init];//不显示空cell
    _tableView.delaysContentTouches = NO;//为no的时候table上面的button有交互效果
    [self.view addSubview:_tableView];
    
    
}
//转换为拼音
- (NSString *)transformToSpellFormChinesName:(NSString *)chineseStu
{
    //将NSString转换为NSMutableString
    NSMutableString *mString = [NSMutableString stringWithFormat:@"%@", chineseStu];
    //转换为带声调的拼音
    CFStringTransform((CFMutableStringRef)mString, NULL, kCFStringTransformMandarinLatin, NO);
    //转换为没有声调的拼音
    CFStringTransform((CFMutableStringRef)mString, NULL, kCFStringTransformStripDiacritics, NO);
    return mString;
}

#pragma mark - Table view data source
//设置分区的title
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    
    if (section == 0) {
        return @"定位城市";
    }else if(section == 1){
        return @"热门城市";
    }else{
        return self.keyArr[section];
    }
}
//设置tableView右边的索引值
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
    return self.keyArr;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.keyArr.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if(section == 0){
        return 1;
    }else if(section == 1){
        return 1;
    }else{
        NSArray *group = [self.cityGroupDic objectForKey:self.keyArr[section]];
        return group.count;
    }
}
- (void)selectCity:(NSString *)city
{
    if (![city isEqualToString:@"定位失败"]) {
        if ([_delegate respondsToSelector:@selector(selectCityName:)]) {
            [_delegate selectCityName:city];
           
        }
        [self.navigationController popViewControllerAnimated:YES];
         _delegate = nil;
        
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    if (indexPath.section == 0||indexPath.section == 1 ) {
        
        
        
        if (indexPath.section == 0) {
            NSString *locationIdentifier = [NSString stringWithFormat:@"%dLocationCell",1];
            CitySelectTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:locationIdentifier];
            if (cell == nil) {
                cell = [[CitySelectTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:locationIdentifier cityCount:1];
            }
            cell.delegate = self;
            
            cell.cityArr = @[self.locationCity?self.locationCity:@"定位失败"];
            
            return cell;
            
        }else{
            NSString *hotIdentifier = [NSString stringWithFormat:@"%ldLocationCell",self.hotArr.count];
            CitySelectTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:hotIdentifier];
            if (cell == nil) {
                cell = [[CitySelectTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:hotIdentifier cityCount:self.hotArr.count];
            }
            
            cell.delegate = self;
            cell.cityArr = self.hotArr;
            return cell;
            
        }
        
        
    }else{
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
        }
        cell.selectionStyle = UITableViewCellSelectionStyleGray;
        NSArray *group = [self.cityGroupDic objectForKey:self.keyArr[indexPath.section]];
        cell.textLabel.text = group[indexPath.row];
        
        return cell;
    }
}
- (void)dealloc
{
    NSLog(@"%@",@"1");
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    if (indexPath.section == 0) {
        return [CitySelectTableViewCell CitySelectTableViewHeightCityCount:1];
    }else if(indexPath.section == 1){
        
        return [CitySelectTableViewCell CitySelectTableViewHeightCityCount:_hotArr.count];
        
    }else{
        return 40;
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 30;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    if ([self isViewLoaded]&&!self.view.window) {
        self.view = nil;
    }
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
